package com.miracle9.game.thread;

import com.miracle9.game.entity.Activity;
import com.miracle9.game.entity.ActivityInfo;
import com.miracle9.game.server.message.MessageSend;
import com.miracle9.game.server.message.StorageMethodOperating;
import com.miracle9.game.service.ActivityService;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("activityThread")
public class ActivityThread extends Thread
{
  private Logger logger = Logger.getLogger(ActivityThread.class);
  @Autowired
  private ActivityService activityService;
  
  @PostConstruct
  public void voidMethod() {}
  
  public void run()
  {
    for (;;)
    {
      ActivityInfo activityInfo = this.activityService.getActivityInfo();
      if (activityInfo == null) {
        return;
      }
      if (activityInfo.getActivityStatus() == 1) {
    	  startBonusRain();
      }
      try
      {
        sleep(2000L);
      }
      catch (InterruptedException e)
      {
        e.printStackTrace();
      }
    }
  }
  
  public void startBonusRain()
  {
    List<Activity> activityList = this.activityService.getActivityList(6);
    
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(11);
    int minute = calendar.get(12);
    int second = calendar.get(13);
    
    int nBonusRainTime = 0;
    for (Activity activity : activityList)
    {
      nBonusRainTime = activity.getCondition1();
      if ((hour == nBonusRainTime) && (minute == 0))
      {
        if ((second >= 0) && (second <= 20))
        {
          this.activityService.redEnvelope(hour);
          
          com.miracle9.game.util.GameParameterAssist.aH.put(Integer.valueOf(hour), Integer.valueOf(minute));
          
          Iterator localIterator2 = com.miracle9.game.util.GameParameterAssist.aR
            .entrySet().iterator();
          while (localIterator2.hasNext())
          {
            Map.Entry entry = (Map.Entry)localIterator2.next();
            
            MessageSend.q.notifyAllOperating(
              new com.miracle9.game.server.message.StorageMethodEntity((IoSession)entry.getValue(), 
              "startBonusRain", new Object[] { Integer.valueOf(300) }));
          }
        }
        try
        {
          Thread.sleep(5000L);
        }
        catch (InterruptedException e)
        {
          e.printStackTrace();
        }
      }
      else if ((hour == nBonusRainTime) && (minute == 5))
      {
        if ((second >= 0) && (second <= 20))
        {
          com.miracle9.game.util.GameParameterAssist.aH.remove(Integer.valueOf(hour));
          
          Iterator localIterator2 = com.miracle9.game.util.GameParameterAssist.aR
            .entrySet().iterator();
          while (localIterator2.hasNext())
          {
            Map.Entry entry = (Map.Entry)localIterator2.next();
            
            MessageSend.q.notifyAllOperating(
              new com.miracle9.game.server.message.StorageMethodEntity((IoSession)entry.getValue(), 
              "overBonusRain", new Object[0]));
          }
        }
        this.activityService.updateAllBonusCount();
        try
        {
          Thread.sleep(600000L);
        }
        catch (InterruptedException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
}
